<template>
  <div class="app-container">
    <el-form ref="user" :model="user" :rules="validateRules" label-width="120px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="user.username"/>
        <p style="margin-bottom: 0;line-height: 20px; color: darkgrey">
          1. 输入的字符数应该在5-15之间;<br>
          2. 用户名不可包含中文;<br>
          3. 特殊字符只能使用"_";<br>
          4. 需要以字母开头
        </p>
      </el-form-item>

      <el-form-item label="昵称">
        <el-input v-model="user.nickName" placeholder="默认为用户名"/>
      </el-form-item>

      <el-form-item label="真实姓名" prop="name">
        <el-input v-model="user.name"/>
      </el-form-item>

      <el-form-item label="性别">
        <el-radio-group v-model="user.gender">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="0">女</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item
        prop="phone"
        label="手机号">
        <el-input v-model="user.phone"/>
      </el-form-item>

      <el-form-item
        prop="email"
        label="邮箱">
        <el-input v-model="user.email"/>
      </el-form-item>

      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import userApi from '@/api/acl/user'

const defaultForm = {
  username: '',
  nickName: '',
  name: '',
  gender: '1',
  phone: '',
  email: ''
}

const usernameRule = (rule, value, callback) => {
  if (value.length < 5) {
    callback(new Error('用户名长度不能小于5位'))
  }
  if (value.length > 15) {
    callback(new Error('用户名长度不能大于15位'))
  }
  if (/([\u4E00-\u9FA5])+/.test(value)) {
    callback(new Error('用户名不能包含中文'))
  }
  if (!/^\w*$/.test(value)) {
    callback(new Error('特殊字符只能使用"_"'))
  }
  if (!/^[a-zA-Z]/.test(value)) {
    callback(new Error('用户名需要以字母开头'))
  }
  callback()
}

const nameRule = (rule, value, callback) => {
  if (!/^([\u4E00-\u9FA5])*$/.test(value)) {
    callback(new Error('真实姓名应只包含中文'))
  }
  callback()
}

const mobileRule = (rule, value, callback) => {
  if (value !== null && value.length !== 0 && !/^1(3|4|5|6|7|8)\d{9}$/.test(value)) {
    callback(new Error('请输入正确的11位手机号'))
  }
  callback()
}

export default {
  data() {
    return {
      user: defaultForm,
      saveBtnDisabled: false,
      validateRules: {
        username: [
          { required: true, trigger: 'blur', message: '请输入合法的用户名' },
          { validator: usernameRule, trigger: 'blur' }
        ],
        name: [
          { required: false },
          { validator: nameRule, trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入正确的11位手机号', trigger: 'blur' },
          { validator: mobileRule, message: '请输入正确的11位手机号', trigger: 'blur' }
        ],
        email: [
          { required: false },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
        ]
      }
    }
  },

  watch: {
    $route(to, from) {
      console.log('路由变化......')
      console.log(to)
      console.log(from)
      this.init()
    }
  },

  created() {
    this.init()
  },

  methods: {

    init() {
      if (this.$route.params && this.$route.params.id) {
        const id = this.$route.params.id
        this.fetchDataById(id)
      } else {
        this.user = { ...defaultForm }
      }
    },

    saveOrUpdate() {
      this.$refs.user.validate(valid => {
        if (valid) {
          this.saveBtnDisabled = true
          if (!this.user.id) {
            this.saveData()
          } else {
            this.updateData()
          }
        }
      })
    },

    saveData() {
      userApi.save(this.user).then(response => {
        if (response.success) {
          this.$message({
            type: 'success',
            message: response.result
          })
          this.$router.push({ path: '/acl/user/list' })
        }
      }).catch(error => {
        this.saveBtnDisabled = false
        console.log(error)
      })
    },

    updateData() {
      userApi.updateById(this.user).then(response => {
        if (response.success) {
          this.$message({
            type: 'success',
            message: response.result
          })
          this.$router.push({ path: '/acl/user/list' })
        }
      }).catch(error => {
        this.saveBtnDisabled = false
        console.log(error)
      })
    },

    fetchDataById(id) {
      userApi.getById(id).then(response => {
        this.user = response.result
      })
    }

  }
}
</script>
